<template>
  <div class="search-box">
    <!-- 位置信息 -->
    <div class="location">
      <div class="city" @click="cityClick">{{currentCity.cityName}}</div>
      <div class="position" @click="positionClick">
        <span class="text">我的位置</span>
        <img src="@/assets/img/home/icon_location.png" alt="">
      </div>
    </div>

    <!-- 日期范围 -->
    <div class="date-range" @click="showCalendar = true">
      <div class="start">
        <span class="tip">入住</span>
        <span>{{startDate}}</span>
      </div>
      <div class="stay">共{{stayCount}}晚</div>
      <div class="end">
        <span class="tip">离店</span>
        <span>{{liveDate}}</span>
      </div>
    </div>
    <!-- 日历 -->
    <van-calendar 
    v-model:show="showCalendar" 
    @confirm="onConfirm" 
    type="range"
    color="#ff9854"
    :formatter="formatter"
    :round="false" 
    :show-confirm="false"
    />

    <div class="price-counter">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>
    <div class="keyword">关键字/位置/民宿</div>

    <!-- 热门建议 -->
    <div class="hot-suggests">
      <template v-for="(item,index) in hotSuggests" :key="index">
        <div class="item">{{item.tagText.text}}</div>
      </template>
    </div>

    <!-- 搜索按钮 -->
    <div class="search-btn">
      <button class="btn" @click="searchClick">开始搜索</button>
    </div>
  </div>
</template>

<script setup>
import useCityStore from "@/stores/modules/city"
import { storeToRefs } from "pinia";
import { ref } from "vue";
import { useRouter } from "vue-router"

import { formatMonthDay, getDiffDays } from '@/utils/format_Date'
import useHomeStore from '@/stores/modules/home'

const router = useRouter()
const cityClick = () => {
  router.push('/city')
}

const positionClick = () => {
  navigator.geolocation.getCurrentPosition(res => {
    console.log("获取位置成功", res)
  },err => {
    console.log("获取位置失败", err)
  },{
    enableHighAccuracy: true,
    timeout: 5000,
    maximumAge: 0
  })
}

//获取城市
const cityStore = useCityStore()
const { currentCity } = storeToRefs(cityStore)

//日期范围的处理
const nowDate = new Date()
const newDate = new Date()
newDate.setDate(nowDate.getDate() + 1)
const startDate = ref(formatMonthDay(new Date()))
const liveDate = ref(formatMonthDay(newDate))
const stayCount = ref(getDiffDays(nowDate, newDate))


//日历
const showCalendar = ref(false)
const onConfirm = (value) => {
  // console.log(value)
  //设置点击日期
  const selectStartDate = value[0]
  const selectEndDate = value[1]
  startDate.value = formatMonthDay(selectStartDate)
  liveDate.value = formatMonthDay(selectEndDate)
  stayCount.value = getDiffDays(selectStartDate, selectEndDate)
  //隐藏日历
  showCalendar.value = false
}
const formatter = (day) => {
      const month = day.date.getMonth() + 1;
      const date = day.date.getDate();
      if (month === 10) {
        if (date === 1 || date === 2 || date === 3 || date === 5 || date === 6 || date === 7) {
          day.topInfo = '国庆节';
        } else if (date === 4) {
          day.topInfo = '重阳节';
        }
      }
      if (day.type === 'start') {
        day.bottomInfo = '入住';
      } else if (day.type === 'end') {
        day.bottomInfo = '离店';
      }

      return day;
    }

  //热门建议
const homeStore = useHomeStore()
homeStore.fetchHotSuggests()
const { hotSuggests } = storeToRefs(homeStore)

//搜索按钮
const searchClick = () => router.push({
  path: '/search',
  query: {
    startDate: startDate.value,
    liveDate: liveDate.value,
    currentCity: currentCity.value.cityName
  }
})

</script>



<style lang="less" scoped>
.search-box {
  --van-calendar-popup-height: 100%;
}

.location {
  display: flex;
  align-items: center;
  height: 44px;
  padding: 0 20px;

  .city {
    flex: 1;
    color: #71777c;
    padding-left: 12px;
  }

  .position {
    width: 74px;
    display: flex;
    align-items: center;

    .text {
      position: relative;
      top: 2px;
      font-size: 12px;
    }

    img {
      margin-left: 5px;
      width: 18px;
      height: 18px;
    }
  }
}

.date-range {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 32px;
  .start {
    display: flex;
    flex-direction: column;
  }
  .stay {
    width: 45px;
    text-align: center;
    background-color: #f0efef;
    border-radius: 10px;
  }
  .end {
    display: flex;
    flex-direction: column;
  }
  .tip {
    color: #71777c;
  }
}

.price-counter {
  display: flex;
  justify-content: space-between;
  line-height: 44px;
  margin: 0 32px;
  color: #71777c;
  height: 44px;
  border-bottom: 1px solid #f2f2f2;
}

.keyword {
  height: 44px;
  line-height: 44px;
  margin: 0 32px;
  color: #71777c;
  border-bottom: 1px solid #f2f2f2;
}

.hot-suggests {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  margin: 0 32px;
  .item {
    padding: 4px 8px;
    background-color: #f2f2f2;
    border-radius: 10px;
    margin: 3px 0;
    font-size: 12px;
  }
}

.search-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  .btn {
    height: 38px;
    color: #fff;
    text-align: center;
    width: 90%;
    background-image: var(--theme-linear-gradient);
    font-size: 18px;
    border-radius: 24px;
    border: none;
    margin: 7px 0;
  }
}
</style>